local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. 'ExternalFun')

local ModifyNickNameLayer = class("ModifyNickNameLayer",function(scene)
    local modifylayer = display.newLayer(cc.c4b(0, 0, 0, 180))
    return modifylayer
end)

function ModifyNickNameLayer:ctor(scene)
    ExternalFun.registerNodeEvent(self)
    self.parentscene = scene
    self:loadCSB()
    self:init()

    self.state = 1  -- 1: input new nickname stage , 2: comfirm new nickname stage

    self:setContentSize(yl.WIDTH,yl.HEIGHT)
end

function ModifyNickNameLayer:loadCSB()
    local _,root = ExternalFun.loadRootCSB("userinfo/modifynicknameLayer.csb",self)
    self.bg = root:getChildByName("Panel_1"):getChildByName("bg")
    ExternalFun.FixScaleUI(self.bg)
	ExternalFun.playOpenLayerAnimation( self.bg )
    local bg = self.bg
    self.btn_close = bg:getChildByName("btn_close")
    self.btn_close1 = bg:getChildByName("btn_close1")
  -- self.txt_title = bg:getChildByName("txt_title")
    self.txt_tips = bg:getChildByName("txt_tips")
    self.btn_sure = bg:getChildByName("btn_sure")
    self.tf_newname = bg:getChildByName("tf_newname")
	
     local newname =  ccui.EditBox:create( self.tf_newname:getContentSize(),ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(self.tf_newname:getPosition())
	:setFontSize(28)
	:setMaxLength(20)
	:setPlaceHolder("最大12个字符")
	bg:addChild(newname)
	self.tf_newname:removeFromParent()
	self.tf_newname = newname
	newname:registerScriptEditBoxHandler(function(event, editbox)
        if event == "return" then
            local editstr = editbox:getText()
            if trim(editstr) == "" then
                showToast(self,"请输入需要修改的昵称")
                return
            end
            local tempstr,_others = ExternalFun.filter_spec_chars(editstr)
            if  _others or (tempstr == ""  and  editstr ~= "") then
                showToast(self,"昵称不能包含特殊字符")
            else
                local tooMax = false
                tempstr,tooMax = ExternalFun.getMaxStringLen(tempstr,12)
                if tooMax  then
                    showToast(self,"昵称最多12个字符")
                end
            end
			editbox:setText(tempstr)

		end
	end)
	
    local btnCallback = function(ref,type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect()
			self:onButtonClicked(ref:getTag(),ref)
			ref:setScale(1)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
    end

    self.btn_close:addTouchEventListener(btnCallback)
    self.btn_close1:addTouchEventListener(btnCallback)
    self.btn_sure:addTouchEventListener(btnCallback)

    local events = {"CHANGENICK",}
   self.tb_listen = appdf.initListener(events,handler(self,self.customEventHandler),self)
end
function ModifyNickNameLayer:onExit()
	appdf.removeListener(self.tb_listen)
end
function ModifyNickNameLayer:customEventHandler(event)
    if event.name == 'CHANGENICK' then
        
        if event.data.result == 1 then
            showToast(self,event.data.msg or "",1)
            if self.parentscene then
                self.parentscene:reSetNickname(self.newname)
            end
			self:removeFromParent()
        else 
            showToast(self, event.data.msg or "修改昵称失败,请稍后再试",1)
        end
    end
end

function ModifyNickNameLayer:init()
    self.txt_tips:setVisible(false)
 --   self.txt_title:setString("请输入新昵称:")
end
function ModifyNickNameLayer:tipsFaild()
	--219 63 55
    self.txt_tips:setVisible(true)
	self.txt_tips:setString("昵称过长,请重新输入")
	self.txt_tips:setTextColor(cc.c3b(219,63,55))
end
function ModifyNickNameLayer:onConfirm()
    if self.state == 1 then
    --    self.txt_title:setString("是否将昵称该为:") --232 192 75
		self.txt_tips:setTextColor(cc.c3b(232,192,75))
  
		self.txt_tips:setString("*温馨提示：确定后无法再次修改")
        self.txt_tips:setVisible(true)
        self.state = 2 
    elseif self.state == 2 then
        
        local msg = {}
        msg.newname = self.tf_newname:getText()
        self.newname = msg.newname
        appdf.clientSocket:sendModifyName(msg)
    end
end

function ModifyNickNameLayer:onButtonClicked(tag, ref)
	if ref == self.btn_close or ref == self.btn_close1 then
		self:removeFromParent(true)
	elseif ref == self.btn_sure then
		local tempstr = string.gsub(self.tf_newname:getText(), " ", "")
        if trim(tempstr) == "" then
            showToast(self,"请输入需要修改的昵称")
            return
        end
		if ExternalFun.stringLen(tempstr)>12 then
			self:tipsFaild(self.tf_newname)
			return
        end
		self:onConfirm()
	end
end

return ModifyNickNameLayer